home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / nasanets.zip / SHOW.C < prev    next >
C/C++ Source or Header  |  1990-06-07  |  8KB  |  245 lines

  1. /*=============================*/
  2. /*           NETS              */
  3. /*                             */
  4. /* a product of the AI Section */
  5. /* NASA, Johnson Space Center  */
  6. /*                             */
  7. /* principal author:           */
  8. /*       Paul Baffes           */
  9. /*                             */
  10. /* contributing authors:       */
  11. /*      Bryan Dulock           */
  12. /*      Chris Ortiz            */
  13. /*=============================*/
  14.  
  15.  
  16. /*
  17. ----------------------------------------------------------------------
  18.   Code For Showing Network Structures (Prefix = S_)
  19. ----------------------------------------------------------------------
  20.   This code is divided into 3 major sections:
  21.  
  22.   (1) include files
  23.   (2) externed functions
  24.   (3) subroutines
  25.  
  26.   Each section is further explained below.
  27. ----------------------------------------------------------------------
  28. */
  29.  
  30.  
  31. /*
  32. ----------------------------------------------------------------------
  33.   INCLUDE FILES
  34. ----------------------------------------------------------------------
  35. */
  36. #include "common.h"
  37. #include "weights.h"
  38. #include "layer.h"
  39. #include "net.h"
  40.  
  41.  
  42. /*
  43. ----------------------------------------------------------------------
  44.   EXTERNED FUNCTIONS
  45. ----------------------------------------------------------------------
  46.   Below are the functions defined in other files which are used by the
  47.   code here. They are organized by section.
  48. ----------------------------------------------------------------------
  49. */
  50. extern  Layer        *N_get_layer();
  51. extern  void         L_show_biases();
  52. extern  void         L_show_learn_rates();
  53. extern  Weights      *L_get_weights();
  54. extern  void         W_show_weights();
  55. extern  void         IO_print();
  56. extern  void         IO_reset_more();
  57. extern  int          IO_more();
  58.  
  59. extern  char         IO_str[MAX_LINE_SIZE];
  60.  
  61.  
  62. /*
  63. ======================================================================
  64.   ROUTINES IN SHOWNET.C                                                   
  65. ======================================================================
  66.   The routines in this file are grouped below by function.  Each routine
  67.   is prefixed by the string "S_" indicating that it is defined in the 
  68.   "show.c" file.  The types returned by the routines are also shown 
  69.   here so that cross checking is more easily done between these functions
  70.   and the other files which intern them.
  71.  
  72.  
  73.   Type Returned                 Routine                                 
  74.   -------------                 -------                                 
  75.     void                        S_show_net
  76.     void                        S_show_wts
  77.     void                        S_show_weights
  78.     void                        S_show_biases
  79. ======================================================================
  80. */
  81.  
  82.  
  83. void  S_show_net(ptr_net)
  84. Net  *ptr_net;
  85. /*
  86. ----------------------------------------------------------------------
  87.  This routine prints out the configuration of the net passed in.  The 
  88.   process involves printing out the net ID, then all the layers. The  
  89.   layers are printed out in rows, starting with the input layer, then 
  90.   the output layer, then any hidden layers. Each row has the layer id,
  91.   then the size of the layer (in nodes), then a list of all of its    
  92.   outputs, then a list of all of its inputs.                          
  93. ----------------------------------------------------------------------
  94. */
  95. BEGIN
  96.    void       S_show_wts();
  97.    Layer_lst  *current;
  98.  
  99.    sprintf(IO_str, "\n\nnet ID:        %d", ptr_net->ID);
  100.    IO_print(0);
  101.    sprintf(IO_str, "\nbiases in use: %s\n",
  102.            (ptr_net->use_biases == TRUE) ? "TRUE" : "FALSE");
  103.    IO_print(0);
  104.  
  105.    /*----------------------------------------*/
  106.    /* show the learning rates for the layers */
  107.    /*----------------------------------------*/
  108.    current = ptr_net->hidden_front;
  109.    while (current != NULL) BEGIN
  110.       L_show_learn_rates(current->value);
  111.       current = current->next;
  112.    ENDWHILE
  113.    
  114.    sprintf(IO_str, "\nlayer %d size: %d; ", ptr_net->input_layer->ID,
  115.           ptr_net->input_layer->num_nodes);
  116.    IO_print(0);
  117.    S_show_wts(ptr_net->input_layer);
  118.    sprintf(IO_str, "layer %d size: %d; ", ptr_net->output_layer->ID, 
  119.           ptr_net->output_layer->num_nodes);
  120.    IO_print(0);
  121.    S_show_wts(ptr_net->output_layer);
  122.  
  123.    current = ptr_net->hidden_front;
  124.    while (current->value->ID != OUT_LAYER) BEGIN
  125.       sprintf(IO_str, "layer %d size: %d; ", current->value->ID,
  126.              current->value->num_nodes);
  127.       IO_print(0);
  128.       S_show_wts(current->value);
  129.       current = current->next;
  130.    ENDWHILE
  131.  
  132. END /* S_show_net */
  133.  
  134.  
  135. void   S_show_wts(ptr_layer)
  136. Layer  *ptr_layer;
  137. /*
  138. ----------------------------------------------------------------------
  139.  NOTE THAT THIS ROUTINE DOES NOT PRINT OUT WEIGHT VALUES. All that is 
  140.   done here is to print out a list of the layer ID to which the input 
  141.   layer acts as a source.  Then the same thing is done showing from   
  142.   which the current layer acts as a target.                           
  143. ----------------------------------------------------------------------
  144. */
  145. BEGIN
  146.    Weights_lst  *current;
  147.  
  148.    sprintf(IO_str, "as source to: ");
  149.    IO_print(0);
  150.    current = ptr_layer->out_weights;
  151.    while (current != NULL) BEGIN
  152.       sprintf(IO_str, "%d ", current->value->target_layer->ID);
  153.       IO_print(0);
  154.       current = current->next;
  155.    ENDWHILE
  156.  
  157.    sprintf(IO_str, " as target from: ");
  158.    IO_print(0);
  159.    current = ptr_layer->in_weights;
  160.    while (current != NULL) BEGIN
  161.       sprintf(IO_str, "%d ", current->value->source_layer->ID);
  162.       IO_print(0);
  163.       current = current->next;
  164.    ENDWHILE
  165.    sprintf(IO_str, "\n");
  166.    IO_print(0);
  167.  
  168. END /* S_show_wts */
  169.  
  170.  
  171. void  S_show_weights(ptr_net, source, target)
  172. Net  *ptr_net;
  173. int  source, target;
  174. /*
  175. ----------------------------------------------------------------------
  176.  Given a particular nerual net, a source layer, and a target layer,   
  177.   this routine will print out the weights between those layers, if a  
  178.   set of weights exitst.  If no weights exist between the two layers  
  179.   this guy will print out an error message.                           
  180. ----------------------------------------------------------------------
  181. */
  182. BEGIN
  183.    Layer    *ptr_layer;
  184.    Weights  *ptr_weights;
  185.    
  186.   IO_reset_more();
  187.   
  188.    ptr_layer = N_get_layer(ptr_net, source);
  189.    if (ptr_layer->ID == ERROR) BEGIN
  190.       sprintf(IO_str, "\n*** no source layer found ***\n");
  191.       IO_print(0);
  192.       return;
  193.    ENDIF
  194.  
  195.    ptr_weights = L_get_weights(ptr_layer, target);
  196.    if (ptr_weights->type == ERROR) BEGIN
  197.       sprintf(IO_str, "\n*** no such source,target pair ***\n");
  198.       IO_print(0);
  199.       return;
  200.    ENDIF
  201.  
  202.    sprintf(IO_str, "\n\nWeights FROM layer %d TO layer %d\n",
  203.           ptr_weights->source_layer->ID, ptr_weights->target_layer->ID);
  204.    IO_more(0);
  205.    W_show_weights(ptr_weights);
  206.  
  207. END /* S_show_weights */
  208.  
  209.  
  210. void  S_show_biases(ptr_net, layer_num)
  211. Net  *ptr_net;
  212. int  layer_num;
  213. /*
  214. ----------------------------------------------------------------------
  215.  Given a particular nerual net, a source layer, and a target layer,   
  216.   this routine will print out the weights between those layers, if a  
  217.   set of weights exitst.  If no weights exist between the two layers  
  218.   this guy will print out an error message.                           
  219. ----------------------------------------------------------------------
  220. */
  221. BEGIN
  222.    Layer    *ptr_layer;   
  223.  
  224.    IO_reset_more();
  225.  
  226.    if (layer_num == 0) BEGIN
  227.       sprintf(IO_str, "\n*** input layer has no bias values ***");
  228.       IO_print(0);
  229.       return;
  230.    ENDIF
  231.    
  232.    ptr_layer = N_get_layer(ptr_net, layer_num);
  233.    if (ptr_layer->ID == ERROR) BEGIN
  234.       sprintf(IO_str, "\n*** layer %d not found ***\n", layer_num);
  235.       IO_print(0);
  236.       return;
  237.    ENDIF
  238.  
  239.    sprintf(IO_str, "\n\nBias values for layer %d\n", ptr_layer->ID);
  240.    IO_more(0);
  241.    L_show_biases(ptr_layer);
  242.  
  243. END /* S_show_biases */
  244.  
  245.